﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using AbcCorp.BLO;
using AbcCorp.DTO;

namespace AbcCorp.ProductService
{
    public class ProductService : IProductService
    {
        public bool AddProduct(Product newProduct)
        {
            var productDto = new ProductDto();
            var productBlo = new ProductBlo();
            CopyObject<Product, ProductDto>.CopyProperties(newProduct, productDto);
            return productBlo.AddProduct(productDto);
        }

        public bool UpdateProduct(Product newInfo, int id)
        {
            var productBlo = new ProductBlo();
            var productDto = new ProductDto();
            CopyObject<Product, ProductDto>.CopyProperties(newInfo, productDto);
            return productBlo.UpdateProduct(productDto, id);
        }

        public bool DeleteProduct(int prodid)
        {
            var productBlo = new ProductBlo();
            return productBlo.DeleteProduct(prodid);
        }

        public IList<Product> GetProducts()
        {
            IList<Product> lstProduct = new List<Product>();
            var productBlo = new ProductBlo();
            var lstProductDto = productBlo.GetProducts();

            if(lstProductDto == null) return null;

            foreach (var productDto in lstProductDto)
            {
                var product = new Product();
                CopyObject<ProductDto, Product>.CopyProperties(productDto, product);
                lstProduct.Add(product);
            }
            return lstProduct;
        }

        public Product GetProduct(int productid)
        {
            var productBlo = new ProductBlo();
            var product = new Product();
            var productDto = productBlo.GetProduct(productid);

            if (productDto == null) return null;

            CopyObject<ProductDto, Product>.CopyProperties(productDto, product);
            return product;
        }

        public IList<Product> GetProductByCategory(int catid)
        {
            IList<Product> lstProduct = new List<Product>();
            var productBlo = new ProductBlo();
            var lstProductDto = productBlo.GetProductByCategory(catid);
            
            if (lstProductDto == null) return null;

            foreach (var productDto in lstProductDto)
            {
                var product = new Product();
                CopyObject<ProductDto, Product>.CopyProperties(productDto, product);
                lstProduct.Add(product);
            }

            return lstProduct;
        }

        public IList<Product> FindProduct(string keySearch, int catid)
        {
            IList<Product> lstProduct = new List<Product>();
            var productBlo = new ProductBlo();
            var lstProductDto = productBlo.FindProduct(keySearch, catid);

            if (lstProductDto == null) return null;

            foreach (var productDto in lstProductDto)
            {
                var product = new Product();
                CopyObject<ProductDto, Product>.CopyProperties(productDto, product);
                lstProduct.Add(product);
            }
            return lstProduct;
        }
    }
}
